package servlet;

import Dao.UserDao;
import com.google.gson.Gson;
import model.Result;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/RegisterUserServlet2")
public class RegisterUserServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        if (userName != null && password != null) {
            User user=new User(userName,password);
            UserDao userDao = new UserDao();

            PrintWriter writer=response.getWriter();
            Gson gson=new Gson();
            boolean result=userDao.saveUser(user);
            if(result)
                writer.write(gson.toJson(new Result("ok")));
            else
                writer.write(gson.toJson(new Result("false")));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
